package com.ztgf.commodity.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.commodity.common.config.YmlConfig;
import com.ztgf.commodity.mapper.CommodityCategoryUnitRelationMapper;
import com.ztgf.commodity.models.dto.CommodityCategoryUnitRelationDTO;
import com.ztgf.commodity.models.service.ICommodityCategoryUnitRelationService;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * author: chenyu.wang
 * date: 2020-07-07 11:15
 * Description:
 */
@DubboService
public class CommodityCategoryUnitRelationServiceImpl extends ServiceImpl<CommodityCategoryUnitRelationMapper, CommodityCategoryUnitRelationDTO> implements ICommodityCategoryUnitRelationService {

    @Resource
    private YmlConfig ymlConfig;

    @Override
    public boolean add(Long userId, Long categoryId, String ids) {
        CommodityCategoryUnitRelationDTO dto;
        List<CommodityCategoryUnitRelationDTO> insertList = new ArrayList<>();
        String[] list = ids.split(",");
        for (String id : list) {
            dto = new CommodityCategoryUnitRelationDTO();
            dto.setId(ymlConfig.getPkId());
            dto.setCreateUserId(userId);
            dto.setCategoryId(categoryId);
            dto.setUnitId(Long.valueOf(id));
            insertList.add(dto);
        }
        return this.insertBatch(insertList);
    }

    @Override
    public boolean deleteAccordingToCategoryId(Long categoryId) {
        Wrapper<CommodityCategoryUnitRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("category_id", categoryId);
        return this.delete(wrapper);
    }
}
